home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PsL Monthly 1993 December
/
PSL Monthly Shareware CD-ROM (December 1993).iso
/
prgmming
/
dos
/
c
/
bor_ti.exe
/
TI1031.ASC
< prev
next >
Wrap
Text File
|
1992-10-23
|
18KB
|
727 lines
PRODUCT : Borland C++ NUMBER : 1031
VERSION : 3.x
OS : DOS
DATE : October 23, 1992 PAGE : 1/11
TITLE : Some Graphics primitives using 256-Color VGA Mode.
// A graphics library demonstrating the simplicity of fast
// graphics in 320x200x256-color VGA mode. The code is not
// fully optimized but it is pretty close; an extra 5% or so
// might be squeezed out of the putimage() for example. Be
// careful when altering pseuodoregisters; while doing one
// load the compiler may trash another register which was just
// loaded with something pertinent.
// Functions provided include point, row, and array (image)
// drawing functions, as well as palette functions using BIOS
// and line-drawing and circle-drawing functions.
// Main() demonstrates most of the functions in a pretty and
// interesting way.
void GrInit(void);
void GrClose(void);
unsigned char GetPixel(unsigned int x, unsigned int y);
void PutPixel(unsigned int x, unsigned int y, unsigned char
color);
void FillRow(unsigned x, unsigned y, unsigned char color,
unsigned length);
void PutRow(unsigned x, unsigned y, char *data, unsigned length);
void GetRow(unsigned x, unsigned y, char *data, unsigned length);
void PutImage(unsigned x, unsigned y, char *data, unsigned rows,
unsigned cols);
void GetImage(unsigned x, unsigned y, char *data, unsigned rows,
unsigned cols);
void GetAllPalette(char (*palette)[3]);
void SetAllPalette( char (*palette)[3]);
void Circle(int x, int y, int radius, unsigned char color);
void Line(unsigned x1, unsigned y1, unsigned x2, unsigned y2,
unsigned char color);
#define VIDEOINT 0x10
#pragma inline
PRODUCT : Borland C++ NUMBER : 1031
VERSION : 3.x
OS : DOS
DATE : October 23, 1992 PAGE : 2/11
TITLE : Some Graphics primitives using 256-Color VGA Mode.
// 'dos.h' is essential
#include <dos.h>
// we need 'math.h' for sqrt() in Circle() and abs() in Line()
#include <math.h>
// we need 'conio.h' for getch()
#include <conio.h>
int rowOffset[200]; // as an optimization,
// rowOffset will be used as a table to
// avoid the multiplication column+(320*row)
// when getting the actual memory address of
// a pixel or a row
// GrInit() initializes the rowOffset table and switches video
// modes to 320x200 with 256 colors
void GrInit()
{
int i;
// Pre-setup our multipications for column,
//row -> screen address
for (i=0; i<200; ++i)
{
rowOffset[i]=i*320;
}
// Set video mode to 320x200 --
// sophisticated users may wish to research checking
// for the availability of VGA/MCGA prior to switching
// modes.
_AH=0;
_AL=0x13;
geninterrupt(VIDEOINT);
}
// GrClose sets the video back into text mode
void GrClose()
{
// set video mode 80x25 color text -- we're assuming
// that that's available
PRODUCT : Borland C++ NUMBER : 1031
VERSION : 3.x
OS : DOS
DATE : October 23, 1992 PAGE : 3/11
TITLE : Some Graphics primitives using 256-Color VGA Mode.
// if we're using VGA
_AH=0;
_AL=0x3;
geninterrupt(VIDEOINT);
}
// GetPixel returns the pixel value at a given (x,y) screen
// location
unsigned char GetPixel(unsigned int x, unsigned int y)
{
return *((char far *) 0xA0000000L+ (x+rowOffset[y]));
}
// PutPixel changes the pixel at a given (x,y) screen location
// to value 'color'
void PutPixel( unsigned int x, unsigned int y,
unsigned char color)
{
*((char far *) 0xA0000000L + (x+rowOffset[y]))=color;
}
// FillRow writes a row of 'color' color pixels of length
// 'length' starting at screen location (x,y).
void FillRow( unsigned x, unsigned y, unsigned char color,
unsigned length )
{
_ES=0xA000; // The segment for screen memory
_DI=x+rowOffset[y]; // The offset in screen to write at
_CX=length; // Number of bytes for rep stosb
_AL=color; // The value stosb will store in video
// memory
asm {
cld // set direction flag so DI
// will increment
rep stosb // zap a row of color AL out to
// video memory al->[es:di]
}
PRODUCT : Borland C++ NUMBER : 1031
VERSION : 3.x
OS : DOS
DATE : October 23, 1992 PAGE : 4/11
TITLE : Some Graphics primitives using 256-Color VGA Mode.
}
// PutRow transfers a row of pixels from 'data' to screen
// starting at (x,y) and continuing for 'length'. A rep
// movsw instead of rep movsb might optimize this slightly for
// even-length rows
void PutRow(unsigned x, unsigned y, char *data, unsigned length)
{
// You may remove all lines marked with '***' if 'data' is a
// near pointer (i.e. if 'data' was not allocated from the far
// heap and was not declared as 'far' data.) Remove them all
// if you remove any.
_ES=0xA000;
_DI=x+rowOffset[y]; // the offset in video memory for
// the row start
_SI=FP_OFF(data);
// load AX last since many C
// instructions will trash AX
_AX=FP_SEG(data); // store data's segment in AX for
// now ***
asm {
push ds // Save DS since we're going to
// alter it ***
mov ds,ax // load register from AX (loaded
// above) ***
mov cx,length // we're moving 'length' bytes
// total
cld // clear the direction flag to
// increment si and di
rep movsb // dump the whole row out to video
// memory [ds:si]->[es:di]
pop ds // Restore DS ***
}
}
// GetRow() copies a row of pixels of length 'length' out of
// video memory into the buffer at 'data'. rep movsw might be
// slightly faster here also.
PRODUCT : Borland C++ NUMBER : 1031
VERSION : 3.x
OS : DOS
DATE : October 23, 1992 PAGE : 5/11
TITLE : Some Graphics primitives using 256-Color VGA Mode.
void GetRow(unsigned x, unsigned y, char *data, unsigned length)
{
_SI=x+rowOffset[y]; // the offset in video memory for
// the row start
_ES=FP_SEG(data); // set up ES:DI to point to data
_DI=FP_OFF(data);
_AX=0xA000; // load video segment in AX
asm {
push ds // save DS since we have to use DS
// for copying
mov ds,ax // load DS with segment for video
// memory from AX (0xA000)
mov cx,length // we're moving 'length' bytes
// total
cld // clear the direction flag to
// increment si and di